import json
import time
from pathlib import Path
import requests
from tqdm import tqdm
from bs4 import BeautifulSoup

from .exception import GetError, UnkownGetError

"""
actions to scrape
"""

def get(uri):
    ## GET request with exception handling
    try:
        response = requests.get(uri)
        if response.status_code == 200:
            return response
        else:
            raise GetError(response.status_code)
    except Exception as e:
        raise UnkownGetError(e)


def count_pages(ssid):
    # 从车系ID获取总页数
    print("Counting {} pages".format(ssid))

    # 访问第1页koubei信息，获取总页数
    koubei_page_uri = 'https://koubei.app.autohome.com.cn/autov8.3.5/alibi/seriesalibiinfos-pm2-s{}-st0-p1-s20-isstruct0-o0.json'
    request_uri = koubei_page_uri.format(ssid)
    response = get(request_uri) # Exception handled in get();
    js = json.loads(response.content)
    return js['result']['pagecount']


def collect_kid(ssid, page_count):
    # 遍历所有页数，获取所有的koubei id
    # 返回所有koibei id的列表
    koubei_page_uri = 'https://koubei.app.autohome.com.cn/autov8.3.5/alibi/seriesalibiinfos-pm2-s{}-st0-p{}-s20-isstruct0-o0.json'
    print('Collecting {} kid:'.format(ssid))
    kids = []
    for page in tqdm(range(1, page_count+1)):
        request_uri = koubei_page_uri.format(ssid, page)
        try:
            response = get(request_uri) # Exception handled in get();
            js = json.loads(response.content)
            kid_in_page = [k['Koubeiid'] for k in js['result']['list']]
            kids += kid_in_page
            time.sleep(1)
        except GetError as e:
            print('GET isse {}, page {} skipped.'.format(e, page))
            continue
        except UnkownGetError:
            print("Unexpected issue, page {} skipped.".format(page))
            continue
    return kids

def scrape_koubei(kid):
    # 使用koubei id 请求koubei信息
    # 返回koubei信息的json数据
    koubei_info_uri = 'https://koubei.app.autohome.com.cn/autov8.3.5/alibi/alibiinfobase-pm2-k{}.json'
    request_uri = koubei_info_uri.format(kid)
    response = get(request_uri) # Exception handled in get();
    js = json.loads(response.content)
    return js

def dump_koubei(kids, target_path, ssid):
    # 保存koubei信息到本地，为原始网页的json数据
    target_path = Path(target_path)
    path = target_path.joinpath(str(ssid))
    path.mkdir(parents=True, exist_ok=True)
    print("Dumping {} koubei".format(ssid))
    for kid in tqdm(kids):
        try:
            js = scrape_koubei(kid)
            if js:
                with open(path.joinpath('{}.json'.format(kid)), 'w', encoding='utf-8') as fp:
                    json.dump(js, fp)
            time.sleep(1)
        except GetError as e:
            print('GET isse {}, koubei {} skipped.'.format(e, kid))
            continue
        except UnkownGetError:
            print("Unexpected issue, koubei {} skipped.".format(kid))
            continue

def scrape_user(uid, cookies):
    # 获取用户信息， 包括昵称、性别、年龄、所在地；
    ## 试验性代码，可用，但需要cookies

    user_info_uri = 'https://i.autohome.com.cn/{}/info'
    request_uri = user_info_uri.format(uid)
    
    # 创建一个会话对象
    session = requests.Session()
    
    # 设置Cookie
    cookies = cookies
    
    # 设置请求头
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0'
    }
    
    # 发送请求
    try:
        response = session.get(request_uri, headers=headers, cookies=cookies)
        if response.status_code == 200:
            html = BeautifulSoup(response.content, 'html.parser')
            div = html.find('div', id='divuserinfo', class_='uData')
            user_info_elements = div.find_all('p')
            user_info = { 'name': None, 'gender': None, 'age': None, 'location': None }
            for e in user_info_elements:
                kv = e.get_text()
                k, v = kv.split(':')
                k = k.strip()
                v = v.strip()
                if k =="昵称":
                    user_info['name'] = v
                elif k == '性别':
                    user_info['gender'] = v
                elif k == '年龄':
                    user_info['age'] = v
                elif k == '所在地':
                    user_info['location'] = v
            return user_info
        else:
            raise GetError(response.status_code)
        
    except Exception as e:
        raise UnkownGetError(e)